//
//  ViewController.m
//  CoreAnimation
//
//  Created by 宗丽康 on 2017/5/11.
//  Copyright © 2017年 宗丽康. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self position];
    [self valueKeyFromAni];
    [self tranction];
    [self springAni];
}


-(void)position{
    
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    myView.backgroundColor = [UIColor redColor];
    [self.view addSubview:myView];
    
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"position"];
    ani.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
    //这个属性默认为YES，意味着在指定的时间段完成后，动画就自动的从层上移除了
    ani.removedOnCompletion = NO;
    ani.fillMode = kCAFillModeForwards;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [myView.layer addAnimation:ani forKey:@"position"];
}


//绕正方形移动
-(void)valueKeyFromAni{
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(30, 50, 100, 100)];
    myView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:myView];
    //锚点
   // myView.layer.anchorPoint
    CAKeyframeAnimation *ani = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    ani.duration = 10.0;
    ani.removedOnCompletion = NO;
    ani.fillMode = kCAFillModeForwards;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    NSValue *value = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(300, 200)];
    NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(300, 500)];
    NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(100, 500)];
    NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
    
    ani.values = @[value,value1,value2,value3,value4];
    [myView.layer addAnimation:ani forKey:@"fromPosition"];
}


//CATracation 转场
-(void)tranction{
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 50, 50)];
    myView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:myView];
    
    CATransition *ani = [CATransition animation];
    ani.type = kCATransitionFade;
    ani.subtype = kCATransitionFromLeft;
    ani.duration = 3.0;
    [myView.layer addAnimation:ani forKey:@"tranction"];
}


//弹簧效果
- (void)springAni {
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(100, 400, 50, 50)];
    myView.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:myView];

    CASpringAnimation * ani = [CASpringAnimation animationWithKeyPath:@"bounds"];
    ani.mass = 50.0; //质量，影响图层运动时的弹簧惯性，质量越大，弹簧拉伸和压缩的幅度越大
    
    ani.stiffness = 5000; //刚度系数(劲度系数/弹性系数)，刚度系数越大，形变产生的力就越大，运动越快
    ani.damping = 100.0;//阻尼系数，阻止弹簧伸缩的系数，阻尼系数越大，停止越快
    ani.initialVelocity = 5.f;//初始速率，动画视图的初始速度大小;速率为正数时，速度方向与运动方向一致，速率为负数时，速度方向与运动方向相反
    ani.duration = ani.settlingDuration;
    ani.toValue = [NSValue valueWithCGRect:myView.bounds];
    ani.removedOnCompletion = NO;
    ani.fillMode = kCAFillModeForwards;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [myView.layer addAnimation:ani forKey:@"boundsAni"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
